package com.atguigu.gmall.payment;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;


/**
 * 使用SDK远程调用 支付宝公司对外暴露的网关 接口
 */
public class AlipayDemo {
    //连接支付宝公司网关 接口
    //注意:是商户调支付宝接口!不是用户
    public static void main(String[] args) {
        //1.实例化客户端 支付宝客户端
        //公共参数
        AlipayClient alipayClient = new DefaultAlipayClient(
                //网关地址 沙箱支付宝网关
                "https://openapi.alipaydev.com/gateway.do",
                "2021000118641210",
                //商家私钥 负责签名 并不发送出去
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCG3LgC1rFwJhkU9Zbsb7T0X2qPhlHe6XULSU5BX4IJILvzgMmArT86+SRM80qGZLBF3uK/Y29sb87UxLNkvhPOCPhzXhACFNqZcg41oKCkpJS/QTlilcltMr0EGZ9TUs4ZBWY59VrxpRqAjgDQlNBc2wbpVGV/SHNBiD8r5Lsol/JC3xB5yn37AJkoEeUkvtNV/Tc/LhH2uNlBcYWViZRCHBRhmzK++6If3xvrX/9/Qyp34cwM1I9Zv8CiwrrbOEx9WjNM7jBN0sF518sv10J0e+3k09tJif5hI4IyjhEFtvs9VdvoLb3yUqP3wrvwwhKhctz/gm/dlB5EWMMCqTMZAgMBAAECggEAXEnO4qJo6oYnw+AMV+BZxKFHGtCgVhsMrZyMoM/q/LQZ6pjHS/dCveignsgex7k/p5DPOomhRmN89r1kxhwNEYGyA8Dq04HR1g/nCWvSgtjdscPAsWYvYTrHfnbKQQr5XsYDHsZ4ZcTGThLfG926eNCp+iJ3EpNRzBagpDjymuPhjXUcmynPC3HRIXIBehhTeBPzUrnbyHZQRQk6RW0uPVR0MxhQ102I59MKhrXtz4/1SKZh0+SZBpOInwVx0luF1JFTsFNBFh86wHnkBIJMIOPstvaZq8j/SKSaOj0o/teea8/cGQ6u4AqNNN5DEBLPEBeipYz1ua/4xN+0WS9zAQKBgQDDy0U5yVgDKH1FtudbrNLOJefxzZoRvCMHL1AJhgJodhYOVPp/zp1hOofVbnEpc8C+3S8VbCPYeIocB+V/A4M2QXVUxale5yVCZ7PISzGCagI8SqIl1JMYbwpD8SbFQ6vvl3hmgnmsXAeDeuA/cX6v7hBp0y1IIT91PlWwjciv5QKBgQCwVPLUIWL8ksJdGr/YlUZE19C1SSJ8PiKaS1dISKiTwz2LQfwZO0TFYto67Lgaa5ojfsaQTS1l/P5keeekAaSxIV4EmpdfdUnw3uCsxwUv5yFMnqaEZg/zdAo/oc57LnL+148NOrdnFHVtwcYg5uPbyeevjxA6PORPN70mxcY7JQKBgQCFJf71xU7tFuPaPQAo/+Z7+ps5fTCBTwgjIrGzSEbq8yMcq/2OXNv5+uvU4T5jiixRn+kdEGVwGMkTNuuC7MiWZP19oM4Fk/tqpUtFsp0Aq0bJ6ZdLUryVqS259oz/KX4LeV6Pl7tW017osu89NdmJmsPes5Xfr3yQJsvvPPx1YQKBgHH5pq/ZGLt7PSijFqgu7Kmo+d2D9UdrfkBPPI6bcGN6SniN4R6KAcy4Najqw6FINeyFkGFLkXGf0ustJL4M1eAsWjINnpR0IqQP3Bex2Ld65To6PhXMRrktI0a4FtfmUSxKNsvtTQjS34hfPOqdc2Doo9oFVg7etxBBTgEWyygFAoGAW7Qdgz2Be058QPCorX4fz4EJHjj35WBIHHGLlP+Tf6/1nw9oRKl+1eovezvJ3TD7D5YKWoouiEZ0BJIrKo3MwkieeLLrkCbubh8WlboSo32AgKDi2hc8QsQAE/e1CC8bpJdWJISGxNqC9+Fz1+3dVIQWUTeRSTKrn2xeRUIP1k0=",
                "json",
                "UTF-8",
                //支付宝公司公钥 负责加密 并不发送出去
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA21WlRtj2DqKRyMfooON6OajAi7IPQ1XNIoFQ+J5SqsAY5nyKbn/MQTUZeE7qD9SjH+GxcKIvfpQj2qHcfirAQBtxyvudYkXx3uNNFNPbB8eM3xe8sQ1KCOGGEY1djKnL4lQ6zbdhNUoDHKCSpaki3e+5qPma3yUoZ5WcGmptBeaBuCjY+FiZzEx2fi7ZkzO8+wJOhulP5FxrD6Jhqo3/0yf50FzqM2Hi3DnvUgXjWXM61ovlPXZ4Wnc6NBhMWaE+0lIh+1zKFkvbvsPg/3ah5C09P5qbf7QpVY0kDRkSIdoOdgDyixWTgSDuMRyFaeC4mih+fIZpIoHraz3wX7xDAQIDAQAB",
                //加密方式
                "RSA2");
        //2.接口 统一收单下单并支持接口 alipay/trade.page.pay
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //TODO 异步回调
        request.setNotifyUrl("");
        //TODO 同步回调
        request.setReturnUrl("");

        //请求参数
        JSONObject bizContent = new JSONObject();  //四个必选值
        bizContent.put("out_trade_no", "20210817010101004761");//交易号: 尚品汇是:ATGUIGU+时间戳+随机数
        bizContent.put("total_amount", 100000000);//总金额
        bizContent.put("subject", "测试商品手机");//
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");//商品码-快速销售,目前只支持FAST_INSTANT_TRADE_PAY
        //Calendar calendar = Calendar.getInstance();
        //calendar.add(Calendar.HOUR,1);
        //bizContent.put("time_expire", calendar.getTime());//二维码失效时间
        request.setBizContent(bizContent.toString());
        //3.发出此请求
        AlipayTradePagePayResponse alipayTradePagePayResponse = null;
        try {
            alipayTradePagePayResponse = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (alipayTradePagePayResponse.isSuccess()) {
            System.out.println("调用成功!!!");
            System.out.println(alipayTradePagePayResponse.getBody());
        } else {
            System.out.println("响应失败!!!");
        }

    }
}
